package com.bookmall.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.bookmall.dao.DiscussDao;
import com.bookmall.entity.Discuss;
import com.bookmall.util.DBUtil;


public class DiscussDaoImpl implements DiscussDao {

	@Override
	public boolean insert(Discuss t) throws SQLException {
		Connection c = DBUtil.getConnection();
		String sql = "insert into discuss_tb (orderId,bookId,userId,content) values(?,?,?,?)";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, t.getOrderId());
		ps.setInt(2, t.getBookId());
		ps.setInt(3, t.getUserId());
		ps.setString(4, t.getContent());
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public boolean update(Discuss t) throws SQLException {

		Connection c = DBUtil.getConnection();
		String sql = "update discuss_tb  set orderId  = ?,bookId  = ?,userId  = ?,content = ? where discussId = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, t.getOrderId());
		ps.setInt(2, t.getBookId());
		ps.setInt(3, t.getUserId());
		ps.setString(4, t.getContent());
		ps.setInt(5,t.getDiscussId());
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public boolean delete(int id) throws SQLException {

		Connection c = DBUtil.getConnection();
		String sql = "update discuss_tb  set delete_flag = 1 where discussId=? and delete_flag = 0";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, id);
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public Discuss selectById(int id) throws SQLException {

		Connection c = DBUtil.getConnection();
		String sql = "select orderId,bookId,userId,content,create_time,modify_time from discuss_tb where discussId=? and delete_flag=0";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			Discuss discuss = new Discuss();
			discuss.setDiscussId(id);
			discuss.setOrderId(rs.getInt(1));
			discuss.setBookId(rs.getInt(2));
			discuss.setUserId(rs.getInt(3));
			discuss.setContent(rs.getString(4));
			discuss.setCreateTime(rs.getTimestamp(5).toLocalDateTime());
			if (rs.getTimestamp(6) != null) {
				discuss.setModifyTime(rs.getTimestamp(6).toLocalDateTime());
			}
			DBUtil.closeConnection(c, ps, null);
			return discuss;
		} else {
			DBUtil.closeConnection(c, ps, null);
			return null;
		}
	}

	@Override
	public List<Discuss> selectByPage(int page, int pageSize) throws SQLException {
		Connection c = DBUtil.getConnection();
		List<Discuss> discuss = new ArrayList<>();
		String sql = "select discussId,orderId,bookId,userId,content,create_time,modify_time from discuss_tb where delete_flag=0 order by create_time desc limit ?,?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, (page - 1) * pageSize);
		ps.setInt(2, pageSize);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			Discuss d = new Discuss();
			d.setDiscussId(rs.getInt(1));
			d.setOrderId(rs.getInt(2));
			d.setBookId(rs.getInt(3));
			d.setUserId(rs.getInt(4));
			d.setContent(rs.getString(5));
			d.setCreateTime(rs.getTimestamp(6).toLocalDateTime());
			if (rs.getTimestamp(7) != null) {
				d.setModifyTime(rs.getTimestamp(7).toLocalDateTime());
			}
			discuss.add(d);
		}
		DBUtil.closeConnection(c, ps, null);
		return discuss.isEmpty() ? Collections.emptyList() : discuss;
	}

}
